import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { useUpdateEffect, useLocalStorageState, useIsomorphicLayoutEffect } from "ahooks";
import { isFunction } from "ahooks/lib/utils";
import { useDispatch, useSelector } from "react-redux";
import { KEYCONST } from "../enums";
import { useCallback, useRef } from "react";
import { matchTheme } from "../utils";
import toast from "../compoents/toast";
export var useMaskOpen = function(next) {
    var dispatch = useDispatch();
    useUpdateEffect(function() {
        dispatch.app.SWITCH_MASK_STATUS(!!next);
    }, [
        next
    ]);
};
export var useLocalStorageSetState = function() {
    var _useLocalStorageState = _sliced_to_array(useLocalStorageState(KEYCONST.localStore, {
        defaultValue: {}
    }), 2), state = _useLocalStorageState[0], setState = _useLocalStorageState[1];
    var setMergeState = useCallback(function(patch) {
        setState(function(prevState) {
            var newState = isFunction(patch) ? patch(prevState) : patch;
            return newState ? _object_spread({}, prevState, newState) : prevState;
        });
    }, []);
    return [
        state,
        setMergeState
    ];
};
/** 不用重复获取 theme */ export var useMatchTheme = function() {
    var theme = useSelector(function(state) {
        return state.app.theme;
    });
    return function(m) {
        return matchTheme(theme, m);
    };
};
/** sdk 加载状态(三方登录) 时显示loading */ export var useScriptLoad = function(instances) {
    var successCount = useRef(instances.filter(function(i) {
        return i.load;
    }).length);
    var errorCount = useRef(0);
    var retryInstance = useRef([]);
    var dispatch = useDispatch();
    var handlerLoad = function(success, that) {
        if (success) {
            successCount.current += 1;
            retryInstance.current = retryInstance.current.filter(function(i) {
                return i == that;
            });
        } else {
            errorCount.current += 1;
            retryInstance.current.push(that);
        }
        var totalCount = successCount.current + errorCount.current;
        if (totalCount >= instances.length) {
            dispatch.app.SET_LOADING(false);
            /** 显示错误信息 */ if (errorCount.current) {
                toast.createToast({
                    type: "errToast",
                    message: "sdk load fail,please try later"
                });
            }
        }
    };
    /** 获取实例加载状态 */ var catchLoad = function() {
        if (retryInstance.current.length) {
            dispatch.app.SET_LOADING(true);
            errorCount.current = 0;
        }
    };
    useIsomorphicLayoutEffect(function() {
        var needLoad = instances.filter(function(i) {
            return !i.load;
        });
        if (needLoad.length) {
            needLoad.forEach(function(instance) {
                var _instance_init;
                (_instance_init = instance.init) === null || _instance_init === void 0 ? void 0 : _instance_init.call(instance).catch(function() {
                // handlerLoad(false, instance);
                });
                instance.onLoad = function() {
                    return handlerLoad(true, instance);
                };
                instance.onError = function() {
                    return handlerLoad(false, instance);
                };
            });
            dispatch.app.SET_LOADING(true);
        }
        return function() {
            needLoad.forEach(function(i) {
                i.onLoad = undefined;
                i.onError = undefined;
            });
        };
    }, []);
    return {
        catchLoad: catchLoad
    };
};

 //# sourceMappingURL=index.js.map